#! /bin/sh
# $OpenLDAP$
## This work is part of OpenLDAP Software <http://www.openldap.org/>.
##
## Copyright 1998-2022 The OpenLDAP Foundation.
## All rights reserved.
##
## Redistribution and use in source and binary forms, with or without
## modification, are permitted only as authorized by the OpenLDAP
## Public License.
##
## A copy of this license is available in the file LICENSE in the
## top-level directory of the distribution or, alternatively, at
## <http://www.OpenLDAP.org/license.html>.

echo "running defines.sh"
. $SRCDIR/scripts/defines.sh

echo ""
echo "  This test tracks a case where the -g flag to slapcat fails to work"
echo "  correctly, exporting the subordinate dbs as well as the root db."
echo "  See https://bugs.openldap.org/show_bug.cgi?id=8667 for more information."
echo ""

DBDIR1=$TESTDIR/db.1.a
DBDIR2=$TESTDIR/db.2.a
DBDIR3=$TESTDIR/db.3.a
DBDIR4=$TESTDIR/db.4.a
CFPRO=$TESTDIR/slapd.d

mkdir -p $TESTDIR $DBDIR1 $DBDIR2 $DBDIR3 $DBDIR4 $CFPRO

$SLAPPASSWD -g -n >$CONFIGPWF

ITS=8667
ITSDIR=$DATADIR/regressions/its$ITS

echo "Starting slapd on TCP/IP port $PORT1..."
. $CONFFILTER $BACKEND < $ITSDIR/slapd.ldif > $CONFLDIF
$SLAPADD -F $CFPRO -n 0 -l $CONFLDIF

echo "Populating root database..."
$SLAPADD -F $CFPRO -b "dc=example,dc=com" -l $ITSDIR/root.ldif >> $TESTOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "slapadd failed for root database ($RC)!"
	exit $RC
fi

echo "Populating accounting database..."
$SLAPADD -F $CFPRO -b "dc=accounting,dc=example,dc=com" -l $ITSDIR/accounting.ldif >> $TESTOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "slapadd failed for accounting database ($RC)!"
	exit $RC
fi

echo "Populating administrative database..."
$SLAPADD -F $CFPRO -b "dc=administrative,dc=example,dc=com" -l $ITSDIR/administrative.ldif >> $TESTOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "slapadd failed for administrative database ($RC)!"
	exit $RC
fi

echo "Populating janitorial database..."
$SLAPADD -F $CFPRO -b "dc=janitorial,dc=example,dc=com" -l $ITSDIR/janitorial.ldif >> $TESTOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "slapadd failed for janitorial database ($RC)!"
	exit $RC
fi

echo "Using slapcat -g to export only the root database..."
$SLAPCAT -F $CFPRO -g -b "dc=example,dc=com" -l $TESTDIR/slapcat.out >> $TESTOUT 2>&1
RC=$?

if test $RC != 0 ; then
        echo "slapcat -g failed for root databse ($RC)!"
        exit $RC
fi

echo "Verifying Administrative subordinate is not found..."
grep "dn: ou=Administrative,dc=example,dc=com" $TESTDIR/slapcat.out >> $TESTOUT 2>&1
RC=$?

if test $RC = 0 ; then
        echo "test failed - Administrative subordinate found."
	echo "This is a regression of ITS8667"
        exit 1
fi

echo "Verifying Accounting subordinate is not found..."
grep "dn: ou=Accounting,dc=example,dc=com" $TESTDIR/slapcat.out >> $TESTOUT 2>&1
RC=$?

if test $RC = 0 ; then
        echo "test failed - Accounting subordinate found."
	echo "This is a regression of ITS8667"
        exit 1
fi

echo "Verifying Janitorial subordinate is not found..."
grep "dn: ou=Janitorial,dc=example,dc=com" $TESTDIR/slapcat.out >> $TESTOUT 2>&1
RC=$?

if test $RC = 0 ; then
        echo "test failed - Janitorial subordinate found."
	echo "This is a regression of ITS8667"
        exit 1
fi

echo
echo ">>>>> Test succeeded"

exit 0
